package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.base.BaseFragmentActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.CollectionFragment;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.DetectFragment;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.MineFragment;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.SellFragment;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.StatisticsFragment;
import com.bjesc_b2c.app.appraiset.utils.ExampleUtil;
import com.orhanobut.logger.Logger;
import com.rxretrofit_build.util.CustomSharedPreferences;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;
import rxretrofit_base.model.KeyBean;
/**
 * 主界面
 */
public class MainActivity extends BaseFragmentActivity implements RadioGroup.OnCheckedChangeListener {
    public static final String KEY_TITLE = "title";
    public static final String KEY_MESSAGE = "message";
    public static final String KEY_EXTRAS = "extras";
    public static boolean isForeground = false;
    public static final String MESSAGE_RECEIVED_ACTION = "com.bjsc.jpush.MESSAGE_RECEIVED_ACTION";
    private MessageReceiver mMessageReceiver;
    @Bind(R.id.rb_sell)
    RadioButton sellRb;
    @Bind(R.id.rb_collection)
    RadioButton collectioRb;
    @Bind(R.id.rb_detect)
    RadioButton detectRb;
    @Bind(R.id.rb_mine)
    RadioButton mineRb;
    @Bind(R.id.rg_group)
    RadioGroup indexRg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initViews();
        String uid = CustomSharedPreferences.getString(this, KeyBean.UID.valueOf(), KeyBean.FILE_NAME.valueOf());
        JPushInterface.setAlias(this,3,uid);
    }

    private void initViews() {
        registerMessageReceiver();
        indexRg.setOnCheckedChangeListener(this);
        sellRb.setChecked(true);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click(); //调用双击退出函数
        }
        return false;
    }
    @Override
    protected void onResume() {
        isForeground = true;
        super.onResume();
    }

    @Override
    protected void onPause() {
        isForeground = false;
        super.onPause();
    }
    /**
     * 双击退出函数
     */
    private static Boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            showToast("再按一次退出程序");
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            finish();
            System.exit(0);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {
            case R.id.rb_sell:
                switchContent(SellFragment.class, R.id.fl_content);
                break;
            case R.id.rb_collection:
                switchContent(CollectionFragment.class, R.id.fl_content);
                break;
            case R.id.rb_detect:
                switchContent(DetectFragment.class, R.id.fl_content);
                break;
            case R.id.rb_statistics:
                switchContent(StatisticsFragment.class, R.id.fl_content);
                break;
            case R.id.rb_mine:
                switchContent(MineFragment.class, R.id.fl_content);
                break;
        }
    }
    public void registerMessageReceiver() {
        mMessageReceiver = new MessageReceiver();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(MESSAGE_RECEIVED_ACTION);
        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, filter);
    }

    public class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
                    String messge = intent.getStringExtra(KEY_MESSAGE);
                    String extras = intent.getStringExtra(KEY_EXTRAS);
                    StringBuilder showMsg = new StringBuilder();
                    showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
                    if (!ExampleUtil.isEmpty(extras)) {
                        showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
                        Logger.d(showMsg.toString());
                    }
                }
            } catch (Exception e){
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
        ButterKnife.unbind(this);
    }

}
